
package com.probiz.estoremf.customer.web.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.cart.util.ShoppingCartUtil;
import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.common.model.order.SalesOrder;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.content.service.SystemMessageManager;
import com.probiz.estore.core.controller.BaseController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.customer.service.FavoriteManager;
import com.probiz.estore.customer.service.MembershipManager;
import com.probiz.estore.order.OrderConstants;
import com.probiz.estore.order.model.OrderSearchCriteria;
import com.probiz.estore.order.service.SalesOrderManager;
import com.probiz.estore.webapp.util.RequestContext;

public class CsMainPageController extends BaseController {

	private SalesOrderManager salesOrderManager;
	private MembershipManager membershipManager;
	private SystemMessageManager systemMessageManager;
	private CustomerManager customerManager;
	private FavoriteManager favoriteManager;

	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response){
		Customer customer = customerManager.getByAppUserId(RequestContext.getCurrentUserId());
		request.setAttribute("customer", customer);
		
		Membership membership = membershipManager.getById(customer.getMembershipId());
		request.setAttribute("membership", membership);
		
		//总消费金额
		BigDecimal totalConsumption = salesOrderManager.getCustomerConsumption(RequestContext.getCurrentUserId(), null, null);
		request.setAttribute("totalConsumption", totalConsumption);
		
		//最近一个周消费金额
		Date lastWeek = DateUtil.getStartOfLast7days();
		Date now = new Date();
		BigDecimal lastWeekConsumption = salesOrderManager.getCustomerConsumption(RequestContext.getCurrentUserId(), lastWeek, now);
		request.setAttribute("lastWeekConsumption", lastWeekConsumption);
		
		//未支付订单
		long unPaidOrderCount = salesOrderManager.countUnPaidSalesOrder(RequestContext.getCurrentUserId());
		request.setAttribute("unPaidOrderCount", unPaidOrderCount);
		
		//最新的定单
		OrderSearchCriteria OrderSearchCriteria = new OrderSearchCriteria();
		OrderSearchCriteria.setCustomerId(RequestContext.getCurrentUserId());
		SearchCriteria sc = SearchCriteria.getHqlPagingInstance("",new Object[0],1 ,5, null);
		List<SalesOrder> salesOrderList=salesOrderManager.searchByCriteria(OrderSearchCriteria, sc);
		request.setAttribute("salesOrderList", salesOrderList); 
		
		//已完成的订单数
		long completedOrderCount = salesOrderManager.countCustomerOrder(RequestContext.getCurrentUserId(), OrderConstants.ORDER_STATUS_COMPLETE);
		request.setAttribute("completedOrderCount", completedOrderCount);
		
		//订单数
		long totalOrderCount = salesOrderManager.countCustomerOrder(RequestContext.getCurrentUserId(), new Short("0"));
		request.setAttribute("totalOrderCount", totalOrderCount);
		
		//收藏商品数
		long totalFavoriteCount = favoriteManager.countCustomerFavorite(RequestContext.getCurrentUserId());
		request.setAttribute("totalFavoriteCount", totalFavoriteCount);
		
		ModelAndView mv = new ModelAndView("customer/csIndex");
		Shoppingcart shoppingcart = ShoppingCartUtil.getInstance().getCurrentUserShoppingcart();
		//把购物车里产品的ID传递给JSP页处理
		String sourceIds = shoppingcart.getProductIds();
		mv.addObject("sourceIds", sourceIds);
		mv.addObject("neverReadMessageCount", systemMessageManager.countNeverReadMessage(RequestContext.getCurrentUserId()));
		return mv;
	}


	public void setSalesOrderManager(SalesOrderManager salesOrderManager) {
		this.salesOrderManager = salesOrderManager;
	}

	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}

	public void setSystemMessageManager(
            SystemMessageManager systemMessageManager) {
        this.systemMessageManager = systemMessageManager;
    }

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}

	public void setFavoriteManager(FavoriteManager favoriteManager) {
		this.favoriteManager = favoriteManager;
	}
}